<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Gio_Core_View
{
	private $_vars = array();
	private $_blocks = array();
	private $_blocksName = array('MAIN_CONTENT');
	private $_layoutContent = null;
	private $_adminSection = false;
	
	private $_headTitle = null;
	private $_headMeta = null;
	private $_headKeyword = null;
	private $_headScripts = array();
	private $_headStyles = array();
	
	private static $_instance = null;
	
	/**
	 * @return Gio_Core_View
	 */
	public static function getInstance()
	{
		if (null == self::$_instance) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	public function __construct($blocks = array())
	{
		$this->setBlocks($blocks);
		$configs = Gio_Core_Config_Xml::getConfig();
		
		/**
		 * Not installed GioCMS
		 */
		if (!Gio_Core_Application::_initInstallChecker()) {
			$request = Gio_Core_Request::getInstance();
			$configs->server->static = $request->getBaseUrl();
			$configs->server->resource = $request->getBaseUrl();
			$configs->web->url = $request->getBaseUrl();
		}
		
		$this->APP_STATIC_SERVER  	= (string)$configs->server->static;
		$this->APP_RESOURCE_SERVER  = (string)$configs->server->resource;
		$this->APP_WEB_URL  		= (string)$configs->web->url;
		$this->APP_WEB_NAME  		= (string)$configs->web->name;
		$this->APP_TEMPLATE  		= (string)$configs->web->template;
		$this->APP_SKIN  			= (string)$configs->web->skin;
		$this->APP_META_KEYWORD		= (string)$configs->web->meta->keyword;
		$this->APP_META_DESCRIPTION	= (string)$configs->web->meta->description;
		
		$defaultLanguage 			= (string)$configs->web->language;
		//$lang						= Gio_Core_Request::getInstance()->getParam('lang');
		//$this->APP_LANG				= ($lang && $defaultLanguage != $lang) ? $lang : $defaultLanguage;
		$this->APP_LANG				= $defaultLanguage;
		
		$routes = Gio_Core_Route::getInstance();
		$this->routes  				= $routes;
		
		$string = Gio_Core_String::getInstance();
		$this->STRING 				= $string;
		
		$translator = new Gio_Core_Translator();
		$this->TRANSLATOR = $translator;
		
		if (Gio_Core_Application::_initInstallChecker() == true) {
			$acl = Gio_Core_Acl::getIntance();
			$this->ACL 				= $acl;
		}
		
		$messenger = Gio_Core_Messenger::getInstance();
		$this->MESSENGER			= $messenger;
		
		if (Gio_Core_Application::_initInstallChecker()) {
			$user	   = Gio_Core_Session::getUser();
			$this->USER 				= $user;
		}
		
		/**
		 * JSON
		 */
		$json = new Services_JSON();
		$this->JSON					= $json;
		
		$this->CONFIG				= $configs;
	}
	
	public function url($name, $params = array())
	{
		return Gio_Core_Route::getInstance()->get($name, $params);
	}
	
	public function setBlocks($blocks)
	{
		$this->_blocks = $blocks;
	}
	
	public function getBlocks()
	{
		return $this->_blocks;
	}
	
	public function assign($name, $value = null)
	{
		$this->_vars[$name] = $value;	
		$this->$name = $value;
	}
	
	public function getVar($name)
	{
		return (isset($this->_vars[$name])) ? $this->_vars[$name] : null; 
	}
	
	public function getVars()
	{
		return $this->_vars;
	}
	
	public function block($name)
	{
		$this->_blocks[] = $name;
	}
	
	public function render($file, $fullPath = true)
	{
		$configs = Gio_Core_Config_Xml::getConfig();
		if (!$fullPath) {
			$file = TEMPLATE_DIR . DS .  $this->APP_TEMPLATE . DS . 'layouts' . DS . $file;	
		}
	
		if (!file_exists($file)) {
			Modules_Core_Services_Exception::error('FILE_NOT_FOUND', $file);
		}
		ob_start();
		require $file;
		$output = ob_get_contents();
		ob_get_clean(); 
		if (!$this->getAdminSection() && $configs->cache->compress == 'true') {
			$output = Gio_Core_HtmlCompress::compress($output);
		}
		return $output;
	}
	
	public function layout()
	{
		return $this->_layoutContent;
	}
	
	public function setLayoutContent($content)
	{
		$this->_layoutContent = $content;
	}
	
	public function setAdminSection($value)
	{
		$this->_adminSection = ($value) ? true : false;
		return $this;
	}
	
	public function getAdminSection()
	{
		return $this->_adminSection;
	}
	
	public function getHeadTitle()
	{
		return $this->_headTitle;
	}
	
	public function headTitle($string = null) {
		if ($string != null) {
			$this->_headTitle = $string;
			return $this;
		}
		$return = sprintf('<title>%s</title>', $this->_headTitle);
		return $return;
	}
	
	public function headMeta($string = null) {
		if ($string != null) {
			$this->_headMeta = $string;
			return $this;
		}
		$this->_headMeta = (null == $this->_headMeta) ? $this->APP_META_DESCRIPTION : $this->_headMeta;
		$return = sprintf('<meta name="description" content="%s" />' . "\n", $this->STRING->escape($this->_headMeta));
		return $return;
	}
	
	public function headKeyword($string = null) {
		if ($string != null) {
			$this->_headKeyword = $string;
			return $this;
		}
		$this->_headKeyword = (null == $this->_headKeyword) ? $this->APP_META_KEYWORD : $this->_headKeyword;
		$return = sprintf('<meta name="keywords" content="%s" />' . "\n", $this->STRING->escape($this->_headKeyword));
		return $return;
	}
	
	public function headScript($string = null)
	{
		if ($string != null) {
			$return = (in_array($string, $this->_headScripts)) 
							? null : $this->_headScripts[] = $string;
			return $this;
		}
		$return = null;
		if ($this->_headScripts) {
			for ($i = count($this->_headScripts) - 1; $i >= 0; $i--) {
				/**
				 * 7 is length of http://
				 */
				if (substr($this->_headScripts[$i], 0, 7) != "http://") { 
					$return .= sprintf('<script type="text/javascript" src="%s/%s"></script>' . "\n"
									, $this->APP_STATIC_SERVER, ltrim($this->_headScripts[$i], '/'));
				} else {
					$return .= sprintf('<script type="text/javascript" src="%s"></script>' . "\n"
									, $this->_headScripts[$i]);
				}
			}
		}
		return $return;
	}
	
	public function getHeadScripts()
	{
		return $this->_headScripts;	
	}
	
	public function headStyle($string = null)
	{
		if ($string != null) {
			$return = (in_array($string, $this->_headStyles)) 
							? null : $this->_headStyles[] = $string;
			return $this;
		}
		$return = null;
		if ($this->_headStyles) {
			for ($i = count($this->_headStyles) - 1; $i >= 0; $i--) {
				if (substr($this->_headStyles[$i], 0, 7) != "http://") {
					$return .= sprintf('<link href="%s/%s" rel="stylesheet" type="text/css" />' . "\n"
								, $this->APP_STATIC_SERVER, ltrim($this->_headStyles[$i], '/')); 
				} else {
					$return .= sprintf('<link href="%s" rel="stylesheet" type="text/css" />' . "\n"
								, $this->_headStyles[$i]);
				}
			}
		}
		return $return;
	}
	
	public function getHeadStyles()
	{
		return $this->_headStyles;	
	}
	
	public function generateTitle()
	{
		$string = null;
		if ($this->_headTitle) {
			$string = sprintf('<?php Gio_Core_View::getInstance()->headTitle(\'%s\'); ?>' . "\n", $this->_headTitle);
		}
		return $string;		
	}
	
	public function generateScripts()
	{
		$string = null;
		if ($scripts = $this->_headScripts) {
			foreach ($scripts as $index => $script) {
				$string .= sprintf('<?php Gio_Core_View::getInstance()->headScript(\'%s\'); ?>' . "\n", $script);
			}
		}
		return $string;
	}
	
	public function generateStyles()
	{
		$string = null;
		if ($styles = $this->_headStyles) {
			foreach ($styles as $index => $style) {
				$string .= sprintf('<?php Gio_Core_View::getInstance()->headStyle(\'%s\'); ?>' . "\n", $style);
			}
		}
		return $string;
	}
	
	public function fetch($file)
	{
		if (!file_exists($file)) {
			throw new Exception($file . ': File not found', 3);
		}
		
		foreach ($this->_blocks as $index => $block) {
			if (!in_array($block->block_name, $this->_blocksName)) {
				$blockName = $block->block_name;
				$type = ($block->type == 'widget') ? 'widgets' . DS . $block->name
													 : $block->name;
				switch ($block->type) {
					case 'widget':
						$widgetContent = Gio_Core_Widget::getIntance()
											->setModuleName($block->module)
											->setWidgetName($block->name)
											->setActionName($block->action)
											->dispatch();
						$this->$blockName = $widgetContent;
						break;
					default:
						$blockViewFile = TEMPLATE_DIR . DS . 'default' . DS . 'modules'
								 . DS . $block->module . DS . $type . DS . $block->action . '.phtml';
						$blockContent = $this->render($blockViewFile);
						$this->$blockName = $blockContent;
						break;
				}
			}
		}
		echo $this->render($file);
	}
	
	public function content($content)
	{
		echo $content;
	}
}